#include "finddialog.h"
//////////////////////////////////////////////////////////////////////////

#ifndef _FINDDIALOG_H_
#include "finddialog.h"
#endif

#include "ui_finddialog.h"

//////////////////////////////////////////////////////////////////////////

#ifndef _MAINWINDOW_H_
#include "mainwindow.h"
#endif

#include <QStringList>
#include <QMessageBox>
#include <QStringListModel>

//////////////////////////////////////////////////////////////////////////


FindDialog::FindDialog( QWidget* _parent )
: Dialog(_parent),
  m_ui(new Ui::FindDialog)
{
    // Setup UI
    m_ui->setupUi(this);

    // Setup search button
    connect( m_ui->m_SearchButton, SIGNAL(clicked()), this, SLOT(Search()) );
}


//////////////////////////////////////////////////////////////////////////


FindDialog::~FindDialog()
{
    delete m_ui;
}


//////////////////////////////////////////////////////////////////////////


void FindDialog::Search()
{
    QAbstractItemModel* model = m_ui->m_ResultsListView->model();
    if (model)
    {
        m_ui->m_ResultsListView->setModel(NULL);
        delete model;
    }


    if (m_ui->m_ByHistoryRadioButton->isChecked())
    {
        bool isOk;
        size_t historyNumber = m_ui->m_SearchLineEdit->text().toUInt(&isOk);
        if (!isOk)
        {
            QMessageBox box(QMessageBox::Warning, tr("Error"),
                tr("Invalid history number."),
                QMessageBox::Ok, this);

            box.exec();

            return;
        }

        GetMainWindow()->GetDatabase().FetchPatients(m_FoundPatients, historyNumber);
    }

    if (m_ui->m_BySurnameRadioButton->isChecked())
    {
        QString surname = m_ui->m_SearchLineEdit->text();
        if ( surname.isEmpty() )
            return;
        
        GetMainWindow()->GetDatabase().FetchPatients(m_FoundPatients, surname);
    }

    if ( m_FoundPatients.empty() )
    {
        QMessageBox box(QMessageBox::Warning, tr("Error"),
            tr("Patients not found."),
            QMessageBox::Ok, this);
        box.exec();
        return;
    }

    QStringList results;

    for ( size_t i = 0; i < m_FoundPatients.size(); ++i)
    {
        QString historyNumberStr = QString::number( m_FoundPatients[i].GetHistoryNumber() );
        QString result = historyNumberStr;
        result += " ";
        result += m_FoundPatients[i].GetSurname();
        result += " ";
        result += m_FoundPatients[i].GetName();
        result += " ";
        result += m_FoundPatients[i].GetSecondName();
        result += " ";
        result += m_FoundPatients[i].GetDiagnosis();
        
        results << result;        
    }

    m_ui->m_ResultsListView->setModel( new QStringListModel(results) );

    // If any patient were found -- select first
    if ( !m_FoundPatients.empty() )
        m_ui->m_ResultsListView->setCurrentIndex( m_ui->m_ResultsListView->model()->index(0, 0) );
}


//////////////////////////////////////////////////////////////////////////


void FindDialog::accept()
{
    // Get selection index
    QModelIndex modelIndex = m_ui->m_ResultsListView->currentIndex();
    if ( !modelIndex.isValid() )
    {
        QMessageBox box(QMessageBox::Warning, tr("Error"),
            tr("Please, select item from search results."),
            QMessageBox::Ok, this);

        box.exec();
        return;
    }

    // Get number of selected row
    size_t row = modelIndex.row();
   
    // Set current patient
    GetMainWindow()->SetCurrPatient( m_FoundPatients.at(row) );

    // Close dialog
    QDialog::accept();
}


//////////////////////////////////////////////////////////////////////////


void FindDialog::show()
{
    // Clear search results
    QStringListModel* model = (QStringListModel*)(m_ui->m_ResultsListView->model());
    if (model)
    {
        m_ui->m_ResultsListView->setModel(NULL);
        delete model;
    }

    // Show dialog
    Dialog::show();
}


//////////////////////////////////////////////////////////////////////////
